package org.shangrila.app.wfm.mb;

import java.io.Serializable;
import java.util.List;

import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;

import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;
import org.primefaces.model.diagram.DefaultDiagramModel;
import org.shangrila.app.sys.shiro.SecUtil;
import org.shangrila.comm.helper.StringHelper;
import org.shangrila.wfm.core.WfmEngine;
import org.shangrila.wfm.core.WfmHelper;
import org.shangrila.wfm.eo.WfmService;
import org.shangrila.wfm.eo.WfmServiceLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Named
@SessionScoped
public class WfmServiceMgrMB implements Serializable{
	private static final Logger log = LoggerFactory.getLogger(WfmServiceMgrMB.class);
	private static final long serialVersionUID = 4899456092581896951L;
	private String operator="root";  
	private String serviceid="";  
	private List voList=null ; //当前服务实例列表
	private List voListLog=null ; //服务实例历史列表
	private List voTask=null ; //当前服务实例当前任务列表
	private List voTaskLog=null ; //当前服务实例已经完成任务列表
	private WfmEngine engine = null;	
	private WfmService selectedService;
	private WfmServiceLog selectedServiceLog;
	private DefaultDiagramModel model;

	public WfmServiceMgrMB() {
		engine = WfmHelper.getEngine();
		try{
			operator = SecUtil.getCachedShiroUser().getUserno();
		}
		catch(Exception ex){
			operator="wfm";
		}
		queryServiceList();
	}
	
	//查询流程实例列表
	public String queryServiceList() {
		if(StringHelper.isEmpty(serviceid))
			voList = engine.getWfmdao().findServiceAll();
		else 
			voList = engine.getWfmdao().findServiceByNo(serviceid);

		return null;
	}
	//流程实例列表(LOG)
	public String queryServiceLogList() {
		if(StringHelper.isEmpty(serviceid))
			voListLog = engine.getWfmdao().findServiceLogAll();
		else 
			voListLog = engine.getWfmdao().findServiceLogByNo(serviceid);
		
		return null;
	}
	
	//处理行选择
   public void onServiceRowSelect(SelectEvent event) {
    	selectedService = (WfmService) event.getObject();
    	log.info("selectedService="+selectedService.getId());
	    this.model = WfmDiagramModel.genProcessDiagramModel(selectedService.getWfmProcess(),selectedService.getCurrentNodeName()); //获取流程图模型
	    //model.findElement(selectedService.getCurrentNodeName()).setStyleClass("ui-diagram-focus");//设置当前节点的特殊颜色
	    queryTaskList();
	    queryTaskLogList();
   }
 
    public void onServiceRowUnselect(UnselectEvent event) {
    }
    
	//处理行选择
    public void onServiceLogRowSelect(SelectEvent event) {
    	selectedServiceLog = (WfmServiceLog) event.getObject();	  
 	    this.model = WfmDiagramModel.genProcessDiagramModel(selectedServiceLog.getWfmProcess(),null); //获取流程图模型
 	    setVoTaskLog(engine.getWfmdao().findTaskLogByServiceId(selectedServiceLog.getId()));
    }
  
     public void onServiceLogRowUnselect(UnselectEvent event) {
     }

	//根据当前用户查询待办任务列表
	public String queryTaskList() {	
		setVoTask(engine.getWfmdao().findTaskByServiceId(selectedService.getId()));
		return null;
	}

	//根据当前用户查询待办任务列表(LOG)
	public String queryTaskLogList() {	
		setVoTaskLog(engine.getWfmdao().findTaskLogByServiceId(selectedService.getId()));
		return null;
	}

	//show faces Messages
	public void showMessages(String summary,String detail) {	
		FacesMessage message = new FacesMessage(summary, detail);
		FacesContext.getCurrentInstance().addMessage(null, message);
	}
	public String getOperator() {
		return operator;
	}

	public void setOperator(String operator) {
		this.operator = operator;
	}

	public String getServiceid() {
		return serviceid;
	}

	public void setServiceid(String serviceid) {
		this.serviceid = serviceid;
	}

	public List getVoList() {
		return voList;
	}

	public void setVoList(List voList) {
		this.voList = voList;
	}

	public List getVoTask() {
		return voTask;
	}

	public void setVoTask(List voTask) {
		this.voTask = voTask;
	}

	public List getVoTaskLog() {
		return voTaskLog;
	}

	public void setVoTaskLog(List voTaskLog) {
		this.voTaskLog = voTaskLog;
	}

	public WfmService getSelectedService() {
		return selectedService;
	}

	public void setSelectedService(WfmService selectedService) {
		this.selectedService = selectedService;
	}

	public DefaultDiagramModel getModel() {
		return model;
	}

	public void setModel(DefaultDiagramModel model) {
		this.model = model;
	}

	/**
	 * @return the voListLog
	 */
	public List getVoListLog() {
		return voListLog;
	}

	/**
	 * @param voListLog the voListLog to set
	 */
	public void setVoListLog(List voListLog) {
		this.voListLog = voListLog;
	}

	/**
	 * @return the selectedServiceLog
	 */
	public WfmServiceLog getSelectedServiceLog() {
		return selectedServiceLog;
	}

	/**
	 * @param selectedServiceLog the selectedServiceLog to set
	 */
	public void setSelectedServiceLog(WfmServiceLog selectedServiceLog) {
		this.selectedServiceLog = selectedServiceLog;
	}	
}
